package com.app.Cutofftime.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    private SQLiteDatabase db;

    private static final String dbName = "TimeCut.db";
    private static final int dbVersion = 1;

    private static final String tableUser = "User";
    private static final String tableXinde = "Xinde";

    private static final String attribute_id0 = "input_id0";
    private static final String attribute_name = "input_user";
    private static final String attribute_password = "input_password";

    private static final String attribute_id = "input_id";
    private static final String attribute_title= "input_title";
    private static final String attribute_code = "input_code";
    private static final String attribute_type = "input_type";
    private static final String attribute_description = "input_description";
    private static final String attribute_date = "input_date";
    private static final String attribute_time = "input_time";
    private static final String attribute_percentage = "input_percentage";

    private static final String createUserTable =
            "create table " + tableUser + "(" +
                    attribute_id0 + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
                    attribute_name + " VARCHAR(50), " +
                    attribute_password + " VARCHAR(30) )";

    private static final String createXindeTable=
            "create table " + tableXinde + "(" +
                    attribute_id + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
                    attribute_title + " VARCHAR(50), " +
                    attribute_code + " VARCHAR(50), " +
                    attribute_type + " VARCHAR(30), " +
                    attribute_description + " VARCHAR(255), " +
                    attribute_date + " VARCHAR(30), " +
                    attribute_time + " VARCHAR(50), " +
                    attribute_percentage + " INT)";



    public DatabaseHelper(Context context) {
        super(context, dbName, null, dbVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createUserTable);
        db.execSQL(createXindeTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists "+tableUser);
        db.execSQL("drop table if exists "+tableXinde);
        onCreate(db);
    }

    public Boolean addUser(String username, String password){
        db = this.getWritableDatabase();

        ContentValues cv = new ContentValues();
        cv.put(attribute_name, username);
        cv.put(attribute_password, password);

        long check = db.insert(tableUser, null, cv);
        return (check > 0);
    }

    public Boolean addTimelimit(String title, String code, String type, String description, String date, String time,String percentage){
        db= this.getWritableDatabase();

        ContentValues cv = new ContentValues();
        cv.put(attribute_title, title);
        cv.put(attribute_code, code);
        cv.put(attribute_type, type);

        cv.put(attribute_description, description);
        cv.put(attribute_date, date);
        cv.put(attribute_time, time);
        cv.put(attribute_percentage,percentage);

        long check = db.insert(tableXinde,null,cv);
        return (check > 0);
    }

    public boolean checkUser(String name) {
        String[] cols = {attribute_name};
        String[] match = {name};

        db = this.getReadableDatabase();
        Cursor cursor = db.query(tableUser, cols, attribute_name + "=?", match, null, null, null);
        return (cursor.getCount() > 0);
    }

    public Boolean checkLogin(String username, String password){
        String[] cols = {attribute_name, attribute_password};
        String[] selectArgs = {username, password};

        db= this.getReadableDatabase();

        Cursor cur = db.query(tableUser, cols, attribute_name + "=? AND " + attribute_password + "=?", selectArgs, null, null, null);

        return (cur.getCount() > 0);
    }



    public Cursor viewTimelimit(){
        db = this.getReadableDatabase();
        String query = "SELECT * FROM " +tableXinde;
        Cursor cursor = db.rawQuery(query, null);
        return cursor;
    }

}

